﻿using System;
using System.Windows.Forms;
using EnvDTE;

namespace SPDevExplorer
{
    public partial class SiteExplorer
    {
        private void mnuSetWorkingFolder_Click(object sender, EventArgs e)
        {
            try
            {
                dlgFolderBrowser.ShowNewFolderButton = true;
                dlgFolderBrowser.SelectedPath = ExplorerSettings.Current(_ctlExplorerTree.SelectedNode).WorkingFolder;
                if (dlgFolderBrowser.ShowDialog() == DialogResult.OK)
                {
                    ExplorerSettings.Current(_ctlExplorerTree.SelectedNode).WorkingFolder =
                        dlgFolderBrowser.SelectedPath;
                    ExplorerSettings.Save();
                }
            }
            catch (Exception ex)
            {
                LogEvent(ex);
            }
        }

        // SvdO: Option to explore the working folder so files can be added manually
        private void mnuExploreWorkingFolder_Click(object sender, EventArgs e)
        {
            string workingFolderPath = EnsurePath(_ctlExplorerTree.SelectedNode);
            System.Diagnostics.Process.Start("explorer.exe", "/select, \"" + workingFolderPath + "\"");
        }

        private void mnuRefreshSite_Click(object sender, EventArgs e)
        {
            TreeNode siteRootNode = _ctlExplorerTree.SelectedNode;
            if (siteRootNode != null)
            {
                siteRootNode.Collapse();
                siteRootNode.Nodes.Clear();
                ListFoldersForNode(siteRootNode, "");
                siteRootNode.Expand();
            }
        }

        private void mnuSiteRemove_Click(object sender, EventArgs e)
        {
            try
            {
                string sPath = EnsurePath(_ctlExplorerTree.SelectedNode);
                foreach (string s in _windows.Keys)
                {
                    if (s.StartsWith(sPath))
                    {
                        Window w = _windows[s];
                        w.Close(vsSaveChanges.vsSaveChangesPrompt);
                    }
                }
                // SvdO: node.Text now contains full url, don't prefix with http://
#if false
                _serviceManager.Remove("http://" + _ctlExplorerTree.SelectedNode.Text);
#else
                _serviceManager.Remove(_ctlExplorerTree.SelectedNode.Text);
#endif
                // /SvdO
            }
            catch (Exception ex)
            {
                LogEvent(ex, false);
            }

            _ctlExplorerTree.SelectedNode.Remove();
        }

        private void mnuShowAllFoldersAndFiles_Click(object sender, EventArgs e)
        {
            TreeNode siteRootNode = _ctlExplorerTree.SelectedNode;
            if (siteRootNode != null)
            {
                // set global flag to indicate if all files and folder must be shown or not
                DisplayAllFoldersAndFilesInTree = !DisplayAllFoldersAndFilesInTree;
                    // siteRootNode.ContextMenuStrip.Items["mnuShowAllFoldersAndFiles"].Selected;
                siteRootNode.Collapse();
                siteRootNode.Nodes.Clear();
                ListFoldersForNode(siteRootNode, "");
                siteRootNode.Expand();
            }
        }

        private void mnuSiteSpSettings_Click(object sender, EventArgs e)
        {
            try
            {
                // SvdO: node.Text now contains full url, don't prefix with http://
#if false
                DTE.ItemOperations.Navigate("http://" + _ctlExplorerTree.SelectedNode.Text + "/_layouts/settings.aspx", vsNavigateOptions.vsNavigateOptionsDefault);
#else
                DTE.ItemOperations.Navigate(_ctlExplorerTree.SelectedNode.Text + "/_layouts/settings.aspx",
                                            vsNavigateOptions.vsNavigateOptionsDefault);
#endif
                // /SvdO
            }
            catch (Exception ex)
            {
                LogEvent(ex);
            }
        }
    }
}